package leetcode_day._2021._202105._1120;

import general_class.TreeNode;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/5/18 21:55
 * 二叉树的堂兄弟节点
 * 算法：深度优先遍历
 */
public class _17_993 {

    int a1 = -1, b1 = -1, a2 = -2, b2 = -2;
    public boolean isCousins(TreeNode root, int x, int y) {
        dfs(root, x, y, 0);
        if (a1 != a2 && b1 == b2) return true;
        return false;
    }
    private void dfs(TreeNode root, int x, int y, int deep) {
        if (null == root) return;
        if (null != root.left) {
            if (x == root.left.val) {
                a1 = root.val;
                b1 = deep + 1;
            } else if (y == root.left.val) {
                a2 = root.val;
                b2 = deep + 1;
            }
            dfs(root.left, x, y, deep + 1);
        }
        if (null != root.right) {
            if (x == root.right.val) {
                a1 = root.val;
                b1 = deep + 1;
            } else if (y == root.right.val) {
                a2 = root.val;
                b2 = deep + 1;
            }
            dfs(root.right, x, y, deep + 1);
        }
    }

}
